
*********************************************************************************;

/* 				This file provides codes for Tables 4 & 5	 					*/
/*				Regressions of decomposed SYY on HF and MF flows 				*/

*********************************************************************************;
%let user = namho;

libname rep "C:\Users\~";

data regbase; set rep.mydata; run; 

*** Table 4: Betas and t values ***;

option missing = " ";
%macro print;
data N; set N; Estimate = nValue1; if Label1 = "Used"; run;
data Nobs; set N; Parameter = "N"; keep EstType Parameter Estimate; run;
data r2; set fit; Parameter = "RSQ"; keep EstType Parameter RSquare; rename RSquare = Estimate;  run;
data final; set parest Nobs r2 ; drop EstType StdErr Probt DF; run;
data final&i; set final; rename Estimate = syy_&freq tValue = t_&freq; 
if Parameter in ("b0", "h_con", "a_con") then delete; run;
%mend;

%macro decomp ;

%do i=1 %to 2;
%if &i =1 %then %do; %let freq = low; %let con = high; %end; 
%else %do; %let freq = high; %let con = low; %end;
ods output "Nonlinear GMM Summary of Residual Errors" = fit;
ods output "Nonlinear GMM Parameter Estimates" = parest;
ods output "Estimation Summary Statistics" = N;
proc model data=regbase plot =none;
endo syy_&freq ; exog active_low active_high hfflow_low hfflow_high mktrf amihud tover hml smb; ; instruments _exog_;
syy_&freq =b0 + h_freq *hfflow_&freq +h_con *hfflow_&con + a_freq *active_&freq + a_con *active_&con +b1*mktrf +b2*amihud +b3*tover +b4*hml +b5*smb; ;
fit syy_&freq / gmm kernel=(bart,13,0) vardef=n; run; quit; 
%print;
%end;

data T4_betas; merge final1 final2; run;
%mend;

%decomp ;

proc print data = T4_betas noobs; run;


*** Table 4: Partial R2 ***;

%macro printR2;
data par&i; set parest; keep Variable SqPartCorrTypeII ; 
rename SqPartCorrTypeII = partial_&freq ;run;

data par&i; set par&i;
if Variable in ("hfflow_con", "active_con") then delete; run;
%mend;

%macro R2decomp;

%do i=1 %to 2;
%if &i =1 %then %do; %let freq = low; %let con = high; %end; 
%else %do; %let freq = high; %let con = low; %end;
data r2; set regbase;
rename hfflow_&freq = hfflow_freq active_&freq = active_freq hfflow_&con = hfflow_con active_&con = active_con; run;
ods output "Parameter Estimates" = parest;
proc reg data=r2 plot =none;
model syy_&freq = hfflow_freq hfflow_con active_freq active_con mktrf amihud tover hml smb / scorr2 pcorr2 stb; 
run; quit; 

%printR2;
%end;

data T4_R2s; merge par1 par2 ; run;
%mend;

%R2decomp;

proc print data = T4_R2s noobs; run;



***** Table 5 Panel A: Variations of Factor Returns ;

proc means data = regbase noprint; 
output out = std_temp 
std(syy_low syy_high hfflow_low hfflow_high active_low active_high) = syy_low syy_high hfflow_low hfflow_high active_low active_high; run;

proc transpose data =std_temp out = std; run;

data std; set std; rename col1 = std;
var =  substr(_name_, 1, find(_name_, '_') -1);
freq = substr(_name_, find(_name_, '_') + 1); 
if _name_ in ("_TYPE_", "_FREQ_") then delete;
drop _name_ _label_; run;

data std; set std;
if var = "hfflow" then ftype = "HF";
else if var = "active" then ftype = "MF";
else ftype = ""; run;

data temp; set T4_betas; keep syy_low syy_high;
if Parameter in ("h_freq", "a_freq"); run;

proc transpose data = temp out=temp2; run;

data HF; set temp2; 
rename col1 = beta;
ftype = "HF"; freq = substr(_name_, find(_name_, '_') + 1);
drop _name_ _label_ col2; run;

data MF; set temp2; 
rename col2 = beta;
ftype = "MF"; freq = substr(_name_, find(_name_, '_') + 1);
drop _name_ _label_ col1; run;

data betas; set HF MF; run;

data betas; retain ftype freq beta;
set betas; run;

proc sql;
create table betas2 as select a.*, b.std as std_fund, c.std as std_syy
from betas as a, std as b, std as c 
where a.freq = b.freq and a.ftype = b.ftype and a.freq = c.freq and c.var = "syy"
order by ftype, freq desc; quit;

proc sql;
create table betas3 as select *, sum(std_syy**2) as var_ttl
from betas2 group by ftype; quit;

data PanelA; set betas3;
R2_freq = (beta*std_fund/std_syy)**2; 
VarRatio = std_syy**2/var_ttl;
R2_ttlret = R2_freq*VarRatio;
EMag = beta*std_fund/std_syy;
drop var_ttl;
run;

proc print data = PanelA; run;


*** Table 5 Panel B: Autocorrelation ;

option missing = " ";
%macro autoprint;
data final; set parest ; drop EstType StdErr tValue Probt DF; run;
data final&i; set final; rename Estimate = &yvar;
if Parameter = "b0" then delete; run;
%mend;

%let var1 = syy_low; 
%let var2 = syy_high;
%let var3 = hfflow_low;
%let var4 = hfflow_high;
%let var5 = active_low; 
%let var6 = active_high;

%macro auto;

%do i=1 %to 6;
%let yvar = &&var&i;
%put &yvar;

proc expand data = regbase out = autoreg method = none; id date;
convert &yvar = lag1 / transformout = (lag 1); run;

ods output "Nonlinear GMM Parameter Estimates" = parest;
proc model data=autoreg plot =none;
endo &yvar; exog lag1;
instruments _exog_; parms b0 b1;
&yvar=b0 +b1*lag1;
fit &yvar / gmm kernel=(bart,13,0) vardef=n;
run; quit; 
%autoprint;
%end;

data auto; merge final1 - final6 ; run;

%mend;

%auto;

proc transpose data = auto out=auto2; run;

data auto2; set auto2; rename col1 = auto;
var =  substr(_name_, 1, find(_name_, '_') -1);
freq = substr(_name_, find(_name_, '_') + 1); 
if _name_ in ("_TYPE_", "_FREQ_") then delete;
drop _name_ _label_; run;

data auto2; set auto2;
if var = "hfflow" then ftype = "HF";
else if var = "active" then ftype = "MF";
else ftype = ""; run;

proc sql;
create table auto3 as select a.*, b.auto as rho_fund, c.auto as rho_syy
from PanelA as a, auto2 as b, auto2 as c 
where a.freq = b.freq and a.ftype = b.ftype and a.freq = c.freq and c.var = "syy"
order by ftype, freq desc; quit;

data auto3; set auto3; 
rho_freq = rho_fund*R2_freq;
cont_rho = rho_freq/rho_syy; 
drop R2_freq VarRatio R2_ttlret EMag std_syy; run;

data PanelB; retain ftype freq rho_fund beta std_fund rho_freq rho_syy cont_rho;
set auto3; run;

proc print data = PanelB; run;
